//
//  LGDeviceManagerController.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2025/2/25.
//

#import "LGDeviceManagerController.h"
#import "LGDeviceManagerTabController.h"
#import "LGDeviceInventoryController.h"
@interface LGDeviceManagerController ()<UIScrollViewDelegate>

/**   */
@property (nonatomic , strong)UIScrollView *scrollView;
/**  服务包 */
@property (nonatomic , strong)LGDeviceManagerTabController *serviceVC;
@property (nonatomic , strong)UIButton *serviceBtn;

/**  商城 */
@property (nonatomic , strong)LGDeviceManagerTabController *marketVC;
@property (nonatomic , strong)UIButton *marketBtn;


@end

@implementation LGDeviceManagerController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.fd_prefersNavigationBarHidden = YES;
    [self.view addSubview:self.navigationView];
    [self.navigationView addSubview:self.serviceBtn];
    [self.navigationView addSubview:self.marketBtn];
    self.navigationView.backgroundColor = [UIColor clearColor];
    self.navigationView.rightBtn.titleLabel.font = LGFont(15);
    [self.navigationView.rightBtn setTitle:@"设备库存" forState:UIControlStateNormal];
    [self.navigationView.rightBtn setTitleColor:[UIColor colorWithString:@"#4C87FD"] forState:UIControlStateNormal];
    [self.navigationView.rightBtn addTarget:self action:@selector(goToDeviceRepertory) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.scrollView];
    
    [self addChildViewController:self.serviceVC];
    [self addChildViewController:self.marketVC];
    [self.scrollView addSubview:self.serviceVC.view];
    [self.scrollView addSubview:self.marketVC.view];
    [self tabButtonTouched:self.serviceBtn];
    
}

-(void)tabButtonTouched:(UIButton *)sender{
    sender.selected = YES;
    if (sender == self.serviceBtn) {
        //服务包
        self.marketBtn.selected = NO;
        [self.serviceVC requestNumberData];
        [UIView animateWithDuration:0.3 animations:^{
            self.serviceBtn.titleLabel.font = LGFontWeight(18, UIFontWeightSemibold);
            self.marketBtn.titleLabel.font = LGFont(18);
            self.scrollView.contentOffset = CGPointMake(0, 0);
        }];
        
    }else{
        //商城
        self.serviceBtn.selected = NO;
        [self.marketVC requestNumberData];
        [UIView animateWithDuration:0.3 animations:^{
            self.serviceBtn.titleLabel.font = LGFont(18);
            self.marketBtn.titleLabel.font = LGFontWeight(18, UIFontWeightSemibold);
            self.scrollView.contentOffset = CGPointMake(Screen_W, 0);
        }];
    }
}

//设备库存
-(void)goToDeviceRepertory{
    LGDeviceInventoryController *controller = [[LGDeviceInventoryController alloc]init];
    [self.navigationController pushViewController:controller animated:YES];
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    CGFloat offset_X = scrollView.contentOffset.x;
    if (offset_X < Screen_W/2.0) {
        self.serviceBtn.selected = YES;
        self.marketBtn.selected = NO;
        self.serviceBtn.titleLabel.font = LGFontWeight(18, UIFontWeightSemibold);
        self.marketBtn.titleLabel.font = LGFont(18);
        
    }else{
        self.serviceBtn.selected = NO;
        self.marketBtn.selected = YES;
        self.serviceBtn.titleLabel.font = LGFont(18);
        self.marketBtn.titleLabel.font = LGFontWeight(18, UIFontWeightSemibold);
    }
}

-(void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    self.gradientLayer.frame = CGRectMake(0, 0, Screen_W, viewPix(167));
}


#pragma mark
#pragma mark ====> 创建控件
-(UIButton *)serviceBtn{
    if(!_serviceBtn){
        _serviceBtn = [UIButton buttonWithTitle:@"服务包" titleFont:LGFontWeight(18, UIFontWeightSemibold) normalColor:@"#909399" selectColor:@"#282828"];
        _serviceBtn.frame = CGRectMake(Screen_W/2.0-viewPix(72), statusBarHeight, viewPix(72), 44);
        [_serviceBtn addTarget:self action:@selector(tabButtonTouched:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _serviceBtn;
}

-(UIButton *)marketBtn{
    if(!_marketBtn){
        _marketBtn = [UIButton buttonWithTitle:@"商城" titleFont:LGFontWeight(18, UIFontWeightSemibold) normalColor:@"#909399" selectColor:@"#282828"];
        _marketBtn.frame = CGRectMake(Screen_W/2.0+viewPix(5), statusBarHeight, viewPix(72), 44);
        [_marketBtn addTarget:self action:@selector(tabButtonTouched:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _marketBtn;
}

-(UIScrollView *)scrollView{
    if(!_scrollView){
        _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, topBarHeight, Screen_W, Screen_H-topBarHeight)];
        _scrollView.contentSize = CGSizeMake(Screen_W*2, Screen_H-topBarHeight);
        _scrollView.showsHorizontalScrollIndicator = NO;
//        _scrollView.pagingEnabled = YES;
//        _scrollView.delegate = self;
        _scrollView.scrollEnabled = NO;
    }
    return _scrollView;
}

-(LGDeviceManagerTabController *)serviceVC{
    if(!_serviceVC){
        _serviceVC = [[LGDeviceManagerTabController alloc]init];
        _serviceVC.type = @"0";
        _serviceVC.view.frame = CGRectMake(0, 0, Screen_W, Screen_H-topBarHeight);
        
    }
    return _serviceVC;
}

-(LGDeviceManagerTabController *)marketVC{
    if(!_marketVC){
        _marketVC = [[LGDeviceManagerTabController alloc]init];
        _marketVC.type = @"1";
        _marketVC.view.frame = CGRectMake(Screen_W, 0, Screen_W, Screen_H-topBarHeight);
    }
    return _marketVC;
}

@end

